<?php

namespace App\Http\Controllers\Manage;

use App\Http\Services\LoginService;
use App\Repositories\Manage\MenuRepository;

class LoginController extends ManageBaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $username = request()->post("username", "");
        $password = request()->post("password", "");
        $captchaKey = request()->post("captchaKey", "");
        $captchaCode = request()->post("captchaCode", "");

        if (!app("captcha")->check($captchaCode)) {
            return retFail("验证码错误");
        }

        $loginService = new LoginService();
        try {
            $user = $loginService->login($username, $password);
//            $uid = 1;
            $token = $loginService->authcode($user['id'], 'ENCODE');
            (new MenuRepository())->delMenuReids($user['id']);

            return retSuc([
                'accessToken' => $token,
                'tokenType' => "t1",
                'username' => $username,
                'password' => $token,
                'captchaKey' => $captchaKey,
            ]);
        } catch (\Exception $e) {
            return retFail($e->getMessage());
        }

    }

    public function logout()
    {
        return retSuc();
    }

    /**
     * Api 图形验证码
     */
    public function captcha()
    {
        $url = app('captcha')->create('default', true);
        return retSuc($url);
    }

}
